package com.smile.frame.config.warn.bizWarn;

import cn.hutool.core.date.DateUtil;
import com.smile.frame.common.exception.BaseErrorCodeEnum;
import com.smile.frame.common.exception.BizException;
import com.smile.frame.common.exception.ErrorCodeEnumInterface;
import com.smile.frame.common.result.Result;
import com.smile.frame.config.warn.contsants.dingding.DingTalkUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * @author LGC
 * @date 2021/7/22 11:42
 */
@Aspect
@Slf4j
@Order(2)
@Component
public class BizWarnAspect {
    @Pointcut("@annotation(com.smile.frame.config.warn.bizWarn.BizWarn)")
    public void pointcut() {

    }

    @Around(value = "pointcut() && @annotation(bizWarn)")
    public Object doAroundMethod(ProceedingJoinPoint joinPoint, BizWarn bizWarn) throws Throwable {
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Exception exception) {
            log.error("异常堆栈信息: ", exception);
            ErrorCodeEnumInterface enumInterface = BaseErrorCodeEnum.BASE_FAIL;
            StringBuilder sb = new StringBuilder("[").append(DateUtil.now()).append("]");
            if (exception instanceof BizException) {
                BizException bizException = (BizException) exception;
                enumInterface = bizException.getEnumInterface();
                sb.append("业务异常 desc:[").append(bizWarn.desc()).append("]</font>")
                        .append("\n\n");
                sb.append("enum:[code:").append(enumInterface.getCode())
                        .append(" msg:").append(enumInterface.getMsg()).append("]")
                        .append("\n\n");
                sb.append("exception: ").append(bizException.getMsg());
            } else {
                sb.append("**<font color='#DC143C'>内部异常 desc:[").append(bizWarn.desc()).append("]</font>**")
                        .append("\n\n");
                sb.append("exceptionName: ").append(exception.getClass().getName()).append("\n\n");
                sb.append("exceptionMsg: ").append(exception.getMessage());
            }
            // 发送钉钉
            if (bizWarn.dingTalk_handle()) {
                try {
                    DingTalkUtil.sendText(sb.toString(), bizWarn.desc());
                } catch (Exception e) {
                    log.error("发送告警信息到钉钉异常:", e);
                }
            }
            // 返回值处理
            if (bizWarn.result_handle()) {
                Object newInstance = bizWarn.result_class().newInstance();
                if (newInstance instanceof Result) {
                    Result result = (Result) newInstance;
                    result.setCode(enumInterface.getCode());
                    result.setMsg(enumInterface.getMsg());
                    return result;
                }
            }
        }
        return proceed;
    }


}
